﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Adex;
using Telerik.Web.UI;
using SubSonic.Sugar;
using System.Security;
using MediaMonitor.Common;
using Ionic.Zip;
using System.IO;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using System.Drawing;


namespace MediaMonitor.Users{
public partial class Report_All : Adex.BasePage
{


    //DateTime sDate = Convert.ToDateTime("01/01/2000"), eDate = Convert.ToDateTime("01/01/2000");
    int mediaTypeCode = 0;
    string mediaCodes = ""; 
    string sectorCode = "", subSectorCode = "" , categoryCode = "";
    string advertiserCode = "", brandCode = "", productCode = "", versionCode = "", agencyCode = "";
    int sTime = 0, eTime = 0;  
    int sPrice = 0, ePrice = 0;
    bool firstAppearanceOnly = false;
    bool downloadOnly = false;
    bool downloadOnlyProduct = false;
    bool downloadOnlyAdv = false;
    int mtc = 0;


    protected void Page_Init(object sender, EventArgs e)
    {

    }


    protected void Page_Load(object sender, EventArgs e)
    {
        downloadOnly = Utility.ToBool(Utility.GetRequestedVal("down", "0"));
        downloadOnlyProduct = Utility.ToBool(Utility.GetRequestedVal("downpro", "0"));
        downloadOnlyAdv = Utility.ToBool(Utility.GetRequestedVal("downadv", "0"));
        
        if (downloadOnly)
        {
            pnlCategory.Visible = true;
            pnlSummaryCategory.Visible = true;
        }
        else
        {
            pnlCategory.Visible = false;
            pnlSummaryCategory.Visible = false;
        }

        if (downloadOnlyProduct)
        {
            PnlProduct.Visible = true;
            pnlSummaryProduct.Visible = true;
        }
        else
        {
            PnlProduct.Visible = false;
            pnlSummaryProduct.Visible = false;
        }
        if (downloadOnlyAdv)
        {
            PnlAdv.Visible = true;
            pnlSummaryAdv.Visible = true;
        }
        else
        {
            PnlAdv.Visible = false;
            pnlSummaryAdv.Visible = false;
        }


        if (!Page.IsPostBack) //On Page Load Inner Grid Bind With All records
        {
            ViewState["dvSummary"] = null;
            Session["cc"] = "";
            Session["adv"] = "";
            Session["brnd"] = "";
            
        }
    }

   



    private void PrepareGridColumns()
    {

        lnkGrp_Adv.Visible = true;
        //lnkGrp_Agnc.Visible = true;
        lnkGrp_Brnd.Visible = true;
        //lnkGrp_Prod.Visible = true;
        lnkGrp_Media.Visible = true;

        GridBoundColumn InvestmentColumn = radGrid.Columns.FindByUniqueName("RealCost") as GridBoundColumn;

        switch (AppUser.GetCountry())
        {
            case 58:
                {
                    InvestmentColumn.DataFormatString = "<small>RD$ <b>{0:N2}</b><small>";
                    break;
                }
            case 218:
                {
                    InvestmentColumn.DataFormatString = "<small>TT$ <b>{0:N2}</b><small>";
                    break;
                }
        }



    }

    



    protected void radGridAll_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        int SDateint = Convert.ToInt32(Utility.GetRequestedVal("sd", "0"));
        int EDateint = Convert.ToInt32(Utility.GetRequestedVal("ed", "0"));

        string sDate = Utility.GetDateFromNumber(Utility.GetRequestedVal("sd", "0")).ToShortDateString();
        string eDate = Utility.GetDateFromNumber(Utility.GetRequestedVal("ed", "0")).ToShortDateString();
        bool searchFromAllMedia_TV = Utility.ToBool(Utility.GetRequestedVal("mcTV", "false"));
        bool searchFromAllMedia_Radio = Utility.ToBool(Utility.GetRequestedVal("mcRD", "false"));
        bool searchFromAllMedia_Cable = Utility.ToBool(Utility.GetRequestedVal("mcCB", "false"));
        bool searchFromAllMedia_Press = Utility.ToBool(Utility.GetRequestedVal("mcPR", "false"));
        bool searchFromAllMedia_OutDoor = Utility.ToBool(Utility.GetRequestedVal("mcOD", "false"));
        bool searchFromAllMedia_Internet = Utility.ToBool(Utility.GetRequestedVal("mcIN", "false"));

        bool searchTV = searchFromAllMedia_TV || Session["SearchMediaCodes_TV"].ToString() != "";
        bool searchRD = searchFromAllMedia_Radio || Session["SearchMediaCodes_RD"].ToString() != "";
        bool searchCB = searchFromAllMedia_Cable || Session["SearchMediaCodes_CB"].ToString() != "";
        bool searchPR = searchFromAllMedia_Press || Session["SearchMediaCodes_PR"].ToString() != "";
        bool searchOD = searchFromAllMedia_OutDoor || Session["SearchMediaCodes_OD"].ToString() != "";
        bool searchIN = searchFromAllMedia_Internet || Session["SearchMediaCodes_IN"].ToString() != "";

        mediaCodes = searchFromAllMedia_TV == false ? Session["SearchMediaCodes_TV"].ToString() : "";
        mediaCodes += searchFromAllMedia_Radio == false ? (Session["SearchMediaCodes_RD"].ToString() == "" ? "" : "," + Session["SearchMediaCodes_RD"].ToString()) : "";
        mediaCodes += searchFromAllMedia_Cable == false ? (Session["SearchMediaCodes_CB"].ToString() == "" ? "" : "," + Session["SearchMediaCodes_CB"].ToString()) : "";
        mediaCodes += searchFromAllMedia_Press == false ? (Session["SearchMediaCodes_PR"].ToString() == "" ? "" : "," + Session["SearchMediaCodes_PR"].ToString()) : "";
        mediaCodes += searchFromAllMedia_OutDoor == false ? (Session["SearchMediaCodes_OD"].ToString() == "" ? "" : "," + Session["SearchMediaCodes_OD"].ToString()) : "";
        mediaCodes += searchFromAllMedia_Internet == false ? (Session["SearchMediaCodes_IN"].ToString() == "" ? "" : "," + Session["SearchMediaCodes_IN"].ToString()) : "";
        
        sectorCode = Utility.GetRequestedVal("s", "");
        subSectorCode = Utility.GetRequestedVal("ss", "");
        categoryCode = Utility.GetRequestedVal("cc", "");
        Session["cc"] = Utility.GetRequestedVal("cc", ""); 
        advertiserCode = Utility.GetRequestedVal("adv", "");
        Session["adv"] = Utility.GetRequestedVal("adv", "");
        brandCode = Utility.GetRequestedVal("brnd", "");
        Session["brnd"] = Utility.GetRequestedVal("brnd", "");
        productCode = Utility.GetRequestedVal("prod", "");
        versionCode = Utility.GetRequestedVal("ver", "");
        agencyCode = Utility.GetRequestedVal("agn", "");

        //sTime = Utility.ToInt(Utility.GetRequestedVal("st", "0"));
        //eTime = Utility.ToInt(Utility.GetRequestedVal("et", "0"));

        sPrice = Utility.ToInt(Utility.GetRequestedVal("sp", "0"));
        ePrice = Utility.ToInt(Utility.GetRequestedVal("ep", "0"));

        firstAppearanceOnly = Utility.ToBool(Utility.GetRequestedVal("fa", "0"));


        objDSAll.SelectParameters["userID"].DefaultValue = AppUser.CurrUserID.ToString();
        objDSAll.SelectParameters["SDateint"].DefaultValue = SDateint.ToString();
        objDSAll.SelectParameters["EDateint"].DefaultValue = EDateint.ToString();
        objDSAll.SelectParameters["sDate"].DefaultValue = sDate.ToString();
        objDSAll.SelectParameters["eDate"].DefaultValue = eDate.ToString();

        objDSAll.SelectParameters["searchTV"].DefaultValue = searchTV.ToString();
        objDSAll.SelectParameters["searchRD"].DefaultValue = searchRD.ToString();
        objDSAll.SelectParameters["searchCB"].DefaultValue = searchCB.ToString();
        objDSAll.SelectParameters["searchPR"].DefaultValue = searchPR.ToString();
        objDSAll.SelectParameters["searchOD"].DefaultValue = searchOD.ToString();
     

        objDSAll.SelectParameters["mediaCodes"].DefaultValue = mediaCodes;
        objDSAll.SelectParameters["sectorName"].DefaultValue = sectorCode;
        objDSAll.SelectParameters["subSectorName"].DefaultValue = subSectorCode;
        objDSAll.SelectParameters["categoryName"].DefaultValue = categoryCode;
        objDSAll.SelectParameters["advertiserName"].DefaultValue = advertiserCode;
        objDSAll.SelectParameters["brandName"].DefaultValue = brandCode;
        objDSAll.SelectParameters["productName"].DefaultValue = productCode;
        objDSAll.SelectParameters["agencyName"].DefaultValue = agencyCode;
        objDSAll.SelectParameters["sPrice"].DefaultValue = sPrice.ToString();
        objDSAll.SelectParameters["ePrice"].DefaultValue = ePrice.ToString();
        objDSAll.SelectParameters["fa"].DefaultValue = firstAppearanceOnly.ToString();
        objDSAll.SelectParameters["searchIN"].DefaultValue = searchIN.ToString();
        radGridAll.DataSourceID = "objDSAll";

        SummaryDIVTOP.Visible = true;


        DataView dvSummary = new DataView();
        dvSummary = (DataView)objDSAll.Select();
        ViewState["dvSummary"] = dvSummary;
       
    }

   private void  Inngrid()
   {
       int SDateint = Convert.ToInt32(Utility.GetRequestedVal("sd", "0"));
       int EDateint = Convert.ToInt32(Utility.GetRequestedVal("ed", "0"));

       string sDate = Utility.GetDateFromNumber(Utility.GetRequestedVal("sd", "0")).ToShortDateString();
       string eDate = Utility.GetDateFromNumber(Utility.GetRequestedVal("ed", "0")).ToShortDateString();
       
        bool searchFromAllMedia_TV = Utility.ToBool(Utility.GetRequestedVal("mcTV", "false"));
        bool searchFromAllMedia_Radio = Utility.ToBool(Utility.GetRequestedVal("mcRD", "false"));
        bool searchFromAllMedia_Cable = Utility.ToBool(Utility.GetRequestedVal("mcCB", "false"));
        bool searchFromAllMedia_Press = Utility.ToBool(Utility.GetRequestedVal("mcPR", "false"));
        bool searchFromAllMedia_OutDoor = Utility.ToBool(Utility.GetRequestedVal("mcOD", "false"));
        bool searchFromAllMedia_Internet = Utility.ToBool(Utility.GetRequestedVal("mcIN", "false"));

        bool searchTV = searchFromAllMedia_TV || Session["SearchMediaCodes_TV"].ToString() != "";
        bool searchRD = searchFromAllMedia_Radio || Session["SearchMediaCodes_RD"].ToString() != "";
        bool searchCB = searchFromAllMedia_Cable || Session["SearchMediaCodes_CB"].ToString() != "";
        bool searchPR = searchFromAllMedia_Press || Session["SearchMediaCodes_PR"].ToString() != "";
        bool searchOD = searchFromAllMedia_OutDoor || Session["SearchMediaCodes_OD"].ToString() != "";
        bool searchIN = searchFromAllMedia_Internet || Session["SearchMediaCodes_IN"].ToString() != "";

        mediaCodes = searchFromAllMedia_TV == false ? Session["SearchMediaCodes_TV"].ToString() : "";
        mediaCodes += searchFromAllMedia_Radio == false ? (Session["SearchMediaCodes_RD"].ToString() == "" ? "" : "," + Session["SearchMediaCodes_RD"].ToString()) : "";
        mediaCodes += searchFromAllMedia_Cable == false ? (Session["SearchMediaCodes_CB"].ToString() == "" ? "" : "," + Session["SearchMediaCodes_CB"].ToString()) : "";
        mediaCodes += searchFromAllMedia_Press == false ? (Session["SearchMediaCodes_PR"].ToString() == "" ? "" : "," + Session["SearchMediaCodes_PR"].ToString()) : "";
        mediaCodes += searchFromAllMedia_OutDoor == false ? (Session["SearchMediaCodes_OD"].ToString() == "" ? "" : "," + Session["SearchMediaCodes_OD"].ToString()) : "";
        mediaCodes += searchFromAllMedia_Internet == false ? (Session["SearchMediaCodes_IN"].ToString() == "" ? "" : "," + Session["SearchMediaCodes_IN"].ToString()) : "";
  
        sectorCode = Utility.GetRequestedVal("s", "") ;
        subSectorCode = Utility.GetRequestedVal("ss", "");
        categoryCode = Utility.GetRequestedVal("cc", "");

        advertiserCode =  Utility.GetRequestedVal("adv", "");
        brandCode =  Utility.GetRequestedVal("brnd", "");
        productCode =  Utility.GetRequestedVal("prod", "");
        versionCode =  Utility.GetRequestedVal("ver", "");
        agencyCode =  Utility.GetRequestedVal("agn", "");

      
         
        sPrice = Utility.ToInt(Utility.GetRequestedVal("sp", "0"));
        ePrice = Utility.ToInt(Utility.GetRequestedVal("ep", "0"));

        firstAppearanceOnly = Utility.ToBool(Utility.GetRequestedVal("fa", "0"));


        objDS.SelectParameters["userID"].DefaultValue = AppUser.CurrUserID.ToString();
        objDS.SelectParameters["SDateint"].DefaultValue = SDateint.ToString();
        objDS.SelectParameters["EDateint"].DefaultValue = EDateint.ToString();
        objDS.SelectParameters["sDate"].DefaultValue = sDate.ToString();
        objDS.SelectParameters["eDate"].DefaultValue = eDate.ToString();

        objDS.SelectParameters["searchTV"].DefaultValue = searchTV.ToString();
        objDS.SelectParameters["searchRD"].DefaultValue = searchRD.ToString();
        objDS.SelectParameters["searchCB"].DefaultValue = searchCB.ToString();
        objDS.SelectParameters["searchPR"].DefaultValue = searchPR.ToString();
        objDS.SelectParameters["searchOD"].DefaultValue = searchOD.ToString();

        objDS.SelectParameters["mediaCodes"].DefaultValue = mediaCodes;
        objDS.SelectParameters["sectorName"].DefaultValue =sectorCode;
        objDS.SelectParameters["subSectorName"].DefaultValue = subSectorCode ;
        objDS.SelectParameters["categoryName"].DefaultValue =  categoryCode ;
        objDS.SelectParameters["advertiserName"].DefaultValue =  advertiserCode ;
        objDS.SelectParameters["brandName"].DefaultValue =  brandCode ;
        objDS.SelectParameters["productName"].DefaultValue =  productCode ; 
        objDS.SelectParameters["agencyName"].DefaultValue = agencyCode ;
        objDS.SelectParameters["sPrice"].DefaultValue = sPrice.ToString();
        objDS.SelectParameters["ePrice"].DefaultValue = ePrice.ToString();
        objDS.SelectParameters["fa"].DefaultValue = firstAppearanceOnly.ToString();
        objDS.SelectParameters["searchIN"].DefaultValue = searchIN.ToString();
   }


   private void InngridExport()
   {
       int SDateint = Convert.ToInt32(Utility.GetRequestedVal("sd", "0"));
       int EDateint = Convert.ToInt32(Utility.GetRequestedVal("ed", "0"));

       string sDate = Utility.GetDateFromNumber(Utility.GetRequestedVal("sd", "0")).ToShortDateString();
       string eDate = Utility.GetDateFromNumber(Utility.GetRequestedVal("ed", "0")).ToShortDateString();
       

       bool searchFromAllMedia_TV = Utility.ToBool(Utility.GetRequestedVal("mcTV", "false"));
       bool searchFromAllMedia_Radio = Utility.ToBool(Utility.GetRequestedVal("mcRD", "false"));
       bool searchFromAllMedia_Cable = Utility.ToBool(Utility.GetRequestedVal("mcCB", "false"));
       bool searchFromAllMedia_Press = Utility.ToBool(Utility.GetRequestedVal("mcPR", "false"));
       bool searchFromAllMedia_OutDoor = Utility.ToBool(Utility.GetRequestedVal("mcOD", "false"));
       bool searchFromAllMedia_Internet = Utility.ToBool(Utility.GetRequestedVal("mcIN", "false"));

       bool searchTV = searchFromAllMedia_TV || Session["SearchMediaCodes_TV"].ToString() != "";
       bool searchRD = searchFromAllMedia_Radio || Session["SearchMediaCodes_RD"].ToString() != "";
       bool searchCB = searchFromAllMedia_Cable || Session["SearchMediaCodes_CB"].ToString() != "";
       bool searchPR = searchFromAllMedia_Press || Session["SearchMediaCodes_PR"].ToString() != "";
       bool searchOD = searchFromAllMedia_OutDoor || Session["SearchMediaCodes_OD"].ToString() != "";
       bool searchIN = searchFromAllMedia_Internet || Session["SearchMediaCodes_IN"].ToString() != "";

       mediaCodes = searchFromAllMedia_TV == false ? Session["SearchMediaCodes_TV"].ToString() : "";
       mediaCodes += searchFromAllMedia_Radio == false ? (Session["SearchMediaCodes_RD"].ToString() == "" ? "" : "," + Session["SearchMediaCodes_RD"].ToString()) : "";
       mediaCodes += searchFromAllMedia_Cable == false ? (Session["SearchMediaCodes_CB"].ToString() == "" ? "" : "," + Session["SearchMediaCodes_CB"].ToString()) : "";
       mediaCodes += searchFromAllMedia_Press == false ? (Session["SearchMediaCodes_PR"].ToString() == "" ? "" : "," + Session["SearchMediaCodes_PR"].ToString()) : "";
       mediaCodes += searchFromAllMedia_OutDoor == false ? (Session["SearchMediaCodes_OD"].ToString() == "" ? "" : "," + Session["SearchMediaCodes_OD"].ToString()) : "";
       mediaCodes += searchFromAllMedia_Internet == false ? (Session["SearchMediaCodes_IN"].ToString() == "" ? "" : "," + Session["SearchMediaCodes_IN"].ToString()) : "";

       sectorCode = Utility.GetRequestedVal("s", "");
       subSectorCode = Utility.GetRequestedVal("ss", "");
       categoryCode = Utility.GetRequestedVal("cc", "");

       advertiserCode = Utility.GetRequestedVal("adv", "");
       brandCode = Utility.GetRequestedVal("brnd", "");
       productCode = Utility.GetRequestedVal("prod", "");
       versionCode = Utility.GetRequestedVal("ver", "");
       agencyCode = Utility.GetRequestedVal("agn", "");



       sPrice = Utility.ToInt(Utility.GetRequestedVal("sp", "0"));
       ePrice = Utility.ToInt(Utility.GetRequestedVal("ep", "0"));

       firstAppearanceOnly = Utility.ToBool(Utility.GetRequestedVal("fa", "0"));


       objDSExport.SelectParameters["userID"].DefaultValue = AppUser.CurrUserID.ToString();
       objDSExport.SelectParameters["SDateint"].DefaultValue = SDateint.ToString();
       objDSExport.SelectParameters["EDateint"].DefaultValue = EDateint.ToString();
       objDSExport.SelectParameters["sDate"].DefaultValue = sDate.ToString();
       objDSExport.SelectParameters["eDate"].DefaultValue = eDate.ToString();

       objDSExport.SelectParameters["searchTV"].DefaultValue = searchTV.ToString();
       objDSExport.SelectParameters["searchRD"].DefaultValue = searchRD.ToString();
       objDSExport.SelectParameters["searchCB"].DefaultValue = searchCB.ToString();
       objDSExport.SelectParameters["searchPR"].DefaultValue = searchPR.ToString();
       objDSExport.SelectParameters["searchOD"].DefaultValue = searchOD.ToString();

       objDSExport.SelectParameters["mediaCodes"].DefaultValue = mediaCodes;
       objDSExport.SelectParameters["sectorName"].DefaultValue = sectorCode;
       objDSExport.SelectParameters["subSectorName"].DefaultValue = subSectorCode;
       objDSExport.SelectParameters["categoryName"].DefaultValue = categoryCode;
       objDSExport.SelectParameters["advertiserName"].DefaultValue = advertiserCode;
       objDSExport.SelectParameters["brandName"].DefaultValue = brandCode;
       objDSExport.SelectParameters["productName"].DefaultValue = productCode;
       objDSExport.SelectParameters["agencyName"].DefaultValue = agencyCode;
       objDSExport.SelectParameters["sPrice"].DefaultValue = sPrice.ToString();
       objDSExport.SelectParameters["ePrice"].DefaultValue = ePrice.ToString();
       objDSExport.SelectParameters["fa"].DefaultValue = firstAppearanceOnly.ToString();
       objDSExport.SelectParameters["searchIN"].DefaultValue = searchIN.ToString();
   }

    protected void radGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
       
        

    }

    protected void ObjDS_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        if (!e.ExecutingSelectCount)
        {
            e.Arguments.MaximumRows = radGrid.PageSize;
            e.InputParameters.Add("e", e);
        }
    }

    protected void ObjDS_SelectingExport(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        if (!e.ExecutingSelectCount)
        {
            e.Arguments.MaximumRows =1;
            e.InputParameters.Add("e", e);
        }
    }


    protected void radGrid_PreRender(object sender, EventArgs e)
    {

       
    }

    protected void radGrid_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == "Add2Cart")
        {

            int pvID = Utility.ToInt(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ProdVersionID"].ToString());
            int pdID = Utility.ToInt(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ProductCode"].ToString());
            decimal price = Utility.ToDecimal(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Price"]);
            string pressFile = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["PressFile"].ToString();
            string outdoorFile = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["OutDoorFile"].ToString();

            int mtCode = Utility.ToInt(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["MediaTypeCode"].ToString());

            String strPressFile = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["PressFile"].ToString();
            String strOutDoorFile = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["OutDoorFile"].ToString();

            String strShortVideoFile = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ShortVideoFile"].ToString();
            String strRadioFile = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["RadioFile"].ToString();

            String strCableFile = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CableFile"].ToString();
            String strStreetGraph = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["StreetGraph"].ToString();
            String rfDate = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["RFDate"].ToString();
            Boolean checkVideoAudioImage=false;

            if (mtCode == 1) //TV
            {
               checkVideoAudioImage= Utility.CheckVideoAvailability(Utility.ToStr(strShortVideoFile));
            }
            else if (mtCode == 3)//Radio
            {
                checkVideoAudioImage = Utility.CheckRadioAvailability(Utility.ToStr(strRadioFile));
            }
            else if (mtCode == 5)//Cable
            {
                checkVideoAudioImage = Utility.CheckCableAvailability(Utility.ToStr(strCableFile));
            }
            else if (mtCode == 6)//Press
            {
                checkVideoAudioImage = Utility.CheckPressAvailability(Utility.ToStr(strPressFile));
            }
            else if (mtCode == 7)//Outdoor and Street Graph
            {
                checkVideoAudioImage = Utility.CheckOutDoorAvailability(Utility.ToStr(strOutDoorFile));
            }
            else if (mtCode == 9)//Internet
            {
                checkVideoAudioImage = Utility.CheckInternetAvailability(Utility.ToStr(strPressFile), Utility.ToStr(rfDate));
            }


            if (checkVideoAudioImage==false)
            {
                AjaxFuncs.MsgBox(this.Page,
                                 "The video, audio or image is temporarily unavailable and not create an order with an empty video, audio or image.");
                return;
            }

            hdnPV.Value = pvID.ToString();
            hdnPD.Value = pdID.ToString();
            hdnPrice.Value = price.ToString();
            hdnMTCode.Value = mtCode.ToString();
            hdnPressFile.Value = pressFile;
            hdnOutDoorFile.Value = outdoorFile;
            hdnRFDate.Value = rfDate;
            int existingOrderWithSameItem = Cart.FindOrderByProdVersion(pvID, AppUser.CurrUserID);

            if (existingOrderWithSameItem > 0)
            {
                string warningMsg = "The video you want to purchase is already present in your Order #<b>" + existingOrderWithSameItem.ToString() + "</b>.<br><br> Do you still want to purchas this video?";
                ltrWarningOrderNum.Text = warningMsg;
                pnl.Visible = true;
            }
            else
            {
                Add2Cart(mtCode, pvID, pdID, price, pressFile, outdoorFile, rfDate);
                //ShowPop(this.Page, false);

            }


        }//end Add2Cart
    }

    private void Add2Cart(int mediaTypeCode, int pvID, int pdID, decimal price, string pressFile, string outdoorFile, string rfDate)
    {


        VProdVersion pv = new VProdVersion("ProdVersionID", pvID);

        string pvName = pv.ProdVersionName;
        string pdName = pv.ProductName;

        string svf = Utility.ToStr(pv.ShortVideoFile);
        string fvf = Utility.ToStr(pv.FullVideoFile);

        bool sv = Utility.ToBool(pv.ShortVideo);
        bool fv = Utility.ToBool(pv.FullVideo);

        string prFile = pressFile;
        string odFile = outdoorFile;

        string rdFile = Utility.ToStr(pv.RadioFile);
        string cbFile = Utility.ToStr(pv.CableFile);
        string inFile = "";

        if (Cart.AddItem(Cart.CreateCartItem(mediaTypeCode, pv.ProdVersionID, pvName, pv.ProductID, pdName, price, svf, fvf, sv, fv, prFile, odFile, rdFile, cbFile, inFile, rfDate)) == true)
        {
            AjaxFuncs.MsgBox(this.Page, "Selected video added to your cart successfully");
        }
        else
        {
            AjaxFuncs.MsgBox(this.Page, "Already added to your cart");
        }

    }


    protected void lnkGrp_Clicked(object sender, EventArgs e)
    {


        LinkButton lnk = ((LinkButton)(sender));
        string grp = lnk.CommandArgument;

        if (lnk.Text.StartsWith("Group"))
        {
            //radGrid.MasterTableView.GroupByExpressions.Add(new GridGroupByExpression(grp + " Group By " + grp));
            radGrid.MasterTableView.GroupLoadMode = GridGroupLoadMode.Server;
            radGrid.MasterTableView.GroupsDefaultExpanded = true;

            radGrid.MasterTableView.GroupByExpressions.Add(grp + " Group By " + grp);
            lnk.ForeColor = System.Drawing.Color.Red;
            lnk.Font.Bold = true;
            lnkClearGroup.Visible = true;
        }

        radGrid.Rebind();

    }

    protected void lnkClearGroup_Clicked(object sender, System.EventArgs e)
    {
        radGrid.MasterTableView.GroupByExpressions.Clear();
        radGrid.Rebind();

        lnkGrp_Adv.ForeColor = lnkGrp_Brnd.ForeColor = lnkGrp_Media.ForeColor = System.Drawing.Color.PowderBlue; //lnkGrp_Agnc.ForeColor

        lnkGrp_Adv.Font.Bold = lnkGrp_Brnd.Font.Bold = lnkGrp_Media.Font.Bold = true; //lnkGrp_Agnc.Font.Bold 

        lnkClearGroup.Visible = false;
    }


  

    protected void btnCancelPurchase_Clicked(object sender, EventArgs e)
    {
        pnl.Visible = false;
    }

    protected void btnContinuePurchase_Clicked(object sender, EventArgs e)
    {
        pnl.Visible = false;
        Add2Cart(Utility.ToInt(hdnMTCode.Value), Utility.ToInt(hdnPV.Value), Utility.ToInt(hdnPD.Value), Utility.ToDecimal(hdnPrice.Value), hdnPressFile.Value, hdnOutDoorFile.Value, hdnRFDate.Value);
    }

    public static void ShowPop(Page aspxPage, bool show)
    {
        try
        {

            AjaxControlToolkit.ModalPopupExtender screen = (AjaxControlToolkit.ModalPopupExtender)aspxPage.FindControl("MPE");


            if ((screen == null))
                return;

            if (show == true)
            {
                screen.Show();

            }
            else
            {
                screen.Hide();
            }
        }
        catch
        {
        }
    }


    protected void radGridAll_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == "GetDetail")
        {
            Int32 MediaType = Utility.ToInt(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["MediaType"].ToString());
            Int32 CountRecord = Utility.ToInt(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CountRecord"].ToString());
            if (CountRecord == 0)
            {}
            else
            {
               
              
               
                Inngrid();
                objDS.SelectParameters["MediaTypeCode"].DefaultValue = MediaType.ToString();
                objDSExport.SelectParameters["MediaTypeCode"].DefaultValue = MediaType.ToString();
                Session["mtc"] =Convert.ToInt32(MediaType.ToString());

                radGrid.DataSourceID = "objDS";
               
                PrepareGridColumns();
                radGridAll.Visible = false;
                SummaryDIVTOP.Visible = false;
               
                InnerDIV.Style.Add("display", "block");

                InnerDIVTOP.Visible = true;
               
                
              
            }

        }//end GetDetail
    }

    protected void lnlBack_Clicked(object sender, System.EventArgs e)
    {
       
        
        InnerDIV.Style.Add("display", "none");
        InnerDIVTOP.Visible = false;
        radGridAll.Visible = true;
        SummaryDIVTOP.Visible = true;
        
     }

    protected void btnDownloadAll_Clicked(object sender, EventArgs e)
    {
        DateTime sDate = Convert.ToDateTime(Utility.GetRequestedVal("sd", "01_01_2000").Replace("_", "/").ToString());
        DateTime eDate = Convert.ToDateTime(Utility.GetRequestedVal("ed", "01_01_2000").Replace("_", "/").ToString());
        Int32 MediaType = 0;
        String CName = String.Empty;
        String CategoryName = String.Empty;
        try
        {
            MediaType = Convert.ToInt32(Session["mtc"].ToString());
            CName = Session["cc"].ToString();
        }
        catch
        {

        }
        DataTable dt = RfController.GetVideosAudiosCategoryName(MediaType, CName, sDate, eDate);

        if (dt.Rows.Count > 0)
        {
            #region[Zip Format]
            Response.Clear();
            // no buffering - allows large zip files to download as they are zipped
            Response.BufferOutput = false;
            String filePath = String.Empty;
            String filePath1 = String.Empty;

            String downloadFileName = dt.Rows[0]["CategoryName"].ToString().Replace("/", "") + ".zip";
            downloadFileName = downloadFileName.Replace(" ", "_");

            Response.ContentType = "application/zip";
            Response.AddHeader("content-disposition", "filename=" + downloadFileName);
            Response.AddHeader("X-Download-Options", "Save As");

            using (ZipFile zip = new ZipFile())
            {
                foreach (DataRow row in dt.Rows)
                {
                    String filePathCheck = String.Empty;
                    String filePathCheck1 = String.Empty;

                    String CatName = String.Empty;


                    if (MediaType.ToString() == "1") //TV
                    {
                        filePath = row["DFile"].ToString();
                        CatName = row["CategoryName"].ToString().Replace("/", "");
                        CatName = CatName.Replace(" ", "_"); ;
                        filePathCheck = Server.MapPath("../videos/" + filePath);
                    }

                    else if ((MediaType.ToString() == "3") || (MediaType.ToString() == "5")) //RADIO AND CABLE
                    {
                        filePath = row["DFile"].ToString();
                        CatName = row["CategoryName"].ToString().Replace("/", "");
                        CatName = CatName.Replace(" ", "_"); ;
                        filePathCheck = Server.MapPath("../radio/" + filePath);
                    }

                    else if (MediaType.ToString() == "6") //press
                    {
                        filePath = row["DFile"].ToString();
                        CatName = row["CategoryName"].ToString().Replace("/", "");
                        CatName = CatName.Replace(" ", "_"); ;
                        filePathCheck = Server.MapPath("../press/" + filePath);
                    }
                    else if (MediaType.ToString() == "7") //outdoor
                    {
                        filePath = row["NewOutDoorFile"].ToString();
                        filePath1 = row["StreetGraph"].ToString();
                        CatName = row["CategoryName"].ToString().Replace("/", "");
                        CatName = CatName.Replace(" ", "_"); ;
                        filePathCheck = Server.MapPath("../outdoor/" + filePath);
                        filePathCheck1 = Server.MapPath("../outdoor-street/" + filePath1);
                    }
                    else if (MediaType.ToString() == "8")//News
                    {
                        filePath = row["CropPicture"].ToString();
                        filePath1 = row["FullPagePicture"].ToString();
                        CatName = row["CategoryName"].ToString().Replace("/", "");
                        CatName = CatName.Replace(" ", "_"); ;
                        filePathCheck = Server.MapPath("../news/Cropped/" + filePath);
                        filePathCheck1 = Server.MapPath("../news/FullPage/" + filePath1);
                    }
                    else if (MediaType.ToString() == "9") //internet
                    {
                        filePath = row["DFile"].ToString();
                        CatName = row["CategoryName"].ToString().Replace("/", "");
                        CatName = CatName.Replace(" ", "_"); ;
                        filePathCheck = Server.MapPath("../internet/" + filePath);
                    }


                    if (filePath != "")
                    {
                        if (File.Exists(filePathCheck))
                        {
                            try
                            {
                                zip.AddFile(filePathCheck, CatName);
                            }
                            catch { }
                        }
                    }

                    if (filePath1 != "")
                    {
                        if (File.Exists(filePathCheck1))
                        {
                            try
                            {
                                zip.AddFile(filePathCheck1, CatName);
                            }
                            catch { }
                        }
                    }
                }
                zip.Save(Response.OutputStream);
            }
            Response.Close();
            Response.End();


            #endregion
        }
        else
        {
            AjaxFuncs.MsgBox(this.Page, "No record exists!");
        }
    }

    protected void btnDownloadAllProduct_Clicked(object sender, EventArgs e)
    {

        Int32 MediaType = 0;
        String CName = String.Empty;
        String CategoryName = String.Empty;
        try
        {
            MediaType = Convert.ToInt32(Session["mtc"].ToString());
            CName = Session["brnd"].ToString();
        }
        catch
        {

        }
        DataTable dt = RfController.GetVideosAudiosProductName(MediaType, CName);

        if (dt.Rows.Count > 0)
        {
            #region[Zip Format]
            Response.Clear();
            // no buffering - allows large zip files to download as they are zipped
            Response.BufferOutput = false;
            String filePath = String.Empty;
            String filePath1 = String.Empty;

            String downloadFileName = dt.Rows[0]["BrandName"].ToString().Replace("/", "") + ".zip";
            downloadFileName = downloadFileName.Replace(" ", "_");

            Response.ContentType = "application/zip";
            Response.AddHeader("content-disposition", "filename=" + downloadFileName);

            using (ZipFile zip = new ZipFile())
            {
                foreach (DataRow row in dt.Rows)
                {
                    String filePathCheck = String.Empty;
                    String filePathCheck1 = String.Empty;

                    String CatName = String.Empty;


                    if (MediaType.ToString() == "1") //TV
                    {
                        filePath = row["DFile"].ToString();
                        CatName = row["BrandName"].ToString().Replace("/", "");
                        CatName = CatName.Replace(" ", "_"); ;
                        filePathCheck = Server.MapPath("../videos/" + filePath);
                    }

                    else if ((MediaType.ToString() == "3") || (MediaType.ToString() == "5")) //RADIO AND CABLE
                    {
                        filePath = row["DFile"].ToString();
                        CatName = row["BrandName"].ToString().Replace("/", "");
                        CatName = CatName.Replace(" ", "_"); ;
                        filePathCheck = Server.MapPath("../radio/" + filePath);
                    }

                    else if (MediaType.ToString() == "6") //press
                    {
                        filePath = row["DFile"].ToString();
                        CatName = row["BrandName"].ToString().Replace("/", "");
                        CatName = CatName.Replace(" ", "_"); ;
                        filePathCheck = Server.MapPath("../press/" + filePath);
                    }
                    else if (MediaType.ToString() == "7") //outdoor
                    {
                        filePath = row["NewOutDoorFile"].ToString();
                        filePath1 = row["StreetGraph"].ToString();
                        CatName = row["BrandName"].ToString().Replace("/", "");
                        CatName = CatName.Replace(" ", "_"); ;
                        filePathCheck = Server.MapPath("../outdoor/" + filePath);
                        filePathCheck1 = Server.MapPath("../outdoor-street/" + filePath1);
                    }
                    else if (MediaType.ToString() == "8")//News
                    {
                        filePath = row["CropPicture"].ToString();
                        filePath1 = row["FullPagePicture"].ToString();
                        CatName = row["BrandName"].ToString().Replace("/", "");
                        CatName = CatName.Replace(" ", "_"); ;
                        filePathCheck = Server.MapPath("../news/Cropped/" + filePath);
                        filePathCheck1 = Server.MapPath("../news/FullPage/" + filePath1);
                    }
                    else if (MediaType.ToString() == "9") //internet
                    {
                        filePath = row["DFile"].ToString();
                        CatName = row["BrandName"].ToString().Replace("/", "");
                        CatName = CatName.Replace(" ", "_"); ;
                        filePathCheck = Server.MapPath("../internet/" + filePath);
                    }


                    if (filePath != "")
                    {
                        if (File.Exists(filePathCheck))
                        {
                            try
                            {
                                zip.AddFile(filePathCheck, CatName);
                            }
                            catch { }
                        }
                    }

                    if (filePath1 != "")
                    {
                        if (File.Exists(filePathCheck1))
                        {
                            try
                            {
                                zip.AddFile(filePathCheck1, CatName);
                            }
                            catch { }
                        }
                    }
                }
                zip.Save(Response.OutputStream);
            }
            Response.Close();
            Response.End();


            #endregion
        }
        else
        {
            AjaxFuncs.MsgBox(this.Page, "No record exists!");
        }
    }

    protected void btnDownloadAllAdv_Clicked(object sender, EventArgs e)
    {
        Int32 MediaType = 0;
        String CName = String.Empty;
        String CategoryName = String.Empty;
        try
        {
            MediaType = Convert.ToInt32(Session["mtc"].ToString());
            CName = Session["adv"].ToString();
        }
        catch
        {

        }
        DataTable dt = RfController.GetVideosAudiosAdvertiserName(MediaType, CName);

        if (dt.Rows.Count > 0)
        {
            #region[Zip Format]
          
            String filePath = String.Empty;
            String filePath1 = String.Empty;

            String downloadFileName = dt.Rows[0]["AdvertiserName"].ToString().Replace("/", "") + ".zip";
            downloadFileName = downloadFileName.Replace(" ", "_");
            Response.Clear();
            // no buffering - allows large zip files to download as they are zipped
            Response.BufferOutput = false;
            Response.ContentType = "application/zip";
            Response.AddHeader("content-disposition", "filename=" + downloadFileName);

            using (ZipFile zip = new ZipFile())
            {
                foreach (DataRow row in dt.Rows)
                {
                    String filePathCheck = String.Empty;
                    String filePathCheck1 = String.Empty;

                    String CatName = String.Empty;


                    if (MediaType.ToString() == "1") //TV
                    {
                        filePath = row["DFile"].ToString();
                        CatName = row["AdvertiserName"].ToString().Replace("/", "");
                        CatName = CatName.Replace(" ", "_"); ;
                        filePathCheck = Server.MapPath("../videos/" + filePath);
                    }

                    else if ((MediaType.ToString() == "3") || (MediaType.ToString() == "5")) //RADIO AND CABLE
                    {
                        filePath = row["DFile"].ToString();
                        CatName = row["AdvertiserName"].ToString().Replace("/", "");
                        CatName = CatName.Replace(" ", "_"); ;
                        filePathCheck = Server.MapPath("../radio/" + filePath);
                    }

                    else if (MediaType.ToString() == "6") //press
                    {
                        filePath = row["DFile"].ToString();
                        CatName = row["AdvertiserName"].ToString().Replace("/", "");
                        CatName = CatName.Replace(" ", "_"); ;
                        filePathCheck = Server.MapPath("../press/" + filePath);
                    }
                    else if (MediaType.ToString() == "7") //outdoor
                    {
                        filePath = row["NewOutDoorFile"].ToString();
                        filePath1 = row["StreetGraph"].ToString();
                        CatName = row["AdvertiserName"].ToString().Replace("/", "");
                        CatName = CatName.Replace(" ", "_"); ;
                        filePathCheck = Server.MapPath("../outdoor/" + filePath);
                        filePathCheck1 = Server.MapPath("../outdoor-street/" + filePath1);
                    }
                    else if (MediaType.ToString() == "8")//News
                    {
                        filePath = row["CropPicture"].ToString();
                        filePath1 = row["FullPagePicture"].ToString();
                        CatName = row["AdvertiserName"].ToString().Replace("/", "");
                        CatName = CatName.Replace(" ", "_"); ;
                        filePathCheck = Server.MapPath("../news/Cropped/" + filePath);
                        filePathCheck1 = Server.MapPath("../news/FullPage/" + filePath1);
                    }
                    else if (MediaType.ToString() == "9") //internet
                    {
                        filePath = row["DFile"].ToString();
                        CatName = row["AdvertiserName"].ToString().Replace("/", "");
                        CatName = CatName.Replace(" ", "_"); ;
                        filePathCheck = Server.MapPath("../internet/" + filePath);
                    }


                    if (filePath != "")
                    {
                        if (File.Exists(filePathCheck))
                        {
                            try
                            {
                                zip.AddFile(filePathCheck, CatName);
                            }
                            catch { }
                        }
                    }

                    if (filePath1 != "")
                    {
                        if (File.Exists(filePathCheck1))
                        {
                            try
                            {
                                zip.AddFile(filePathCheck1, CatName);
                            }
                            catch { }
                        }
                    }
                }
                zip.Save(Response.OutputStream);
            }
            Response.Close();
            Response.End();


            #endregion
        }
        else
        {
            AjaxFuncs.MsgBox(this.Page, "No record exists!");
        }
    }
    


    protected void btnDownloadAllSummaryProduct_Clicked(object sender, EventArgs e)
    {

        Int32 checkResponse = 0;

        ZipFile zip = new ZipFile();

        foreach (GridDataItem item in radGridAll.Items)
        {

            Int32 MediaType = 0;
            String CName = String.Empty;
            String CategoryName = String.Empty;

            try
            {
                Label lblMT = (Label)item["MediaType"].FindControl("lblMTN");
                MediaType = Convert.ToInt32(lblMT.Text);
                CName = Utility.GetRequestedVal("brnd", "");


            }
            catch
            {

            }
            DataTable dt = RfController.GetVideosAudiosProductName(MediaType, CName);

            if (dt.Rows.Count > 0)
            {
                #region[Zip Format]
             
                String filePath = String.Empty;
                String filePath1 = String.Empty;

                String downloadFileName = String.Empty;
                String downloadFileName_ = String.Empty;
                downloadFileName_ = dt.Rows[0]["BrandName"].ToString().Replace("/", "") + ".zip";
                downloadFileName = downloadFileName_.Replace(" ", "_");


                // no buffering - allows large zip files to download as they are zipped
                if (checkResponse == 0)
                {
                    Response.Clear();
                    Response.BufferOutput = false;

                    Response.ContentType = "application/zip";
                    Response.AddHeader("content-disposition", "filename=" + downloadFileName);
                }
                checkResponse = 1;


                //using (zip = new ZipFile())
                //{
                  

                   
                    foreach (DataRow row in dt.Rows)
                    {
                        String filePathCheck = String.Empty;
                        String filePathCheck1 = String.Empty;

                        String CatName = String.Empty;


                        if (MediaType.ToString() == "1") //TV
                        {
                            filePath = row["DFile"].ToString();
                            CatName = row["BrandName"].ToString().Replace("/", "");
                            CatName = CatName.Replace(" ", "_"); ;
                            filePathCheck = Server.MapPath("../videos/" + filePath);
                        }

                        else if ((MediaType.ToString() == "3") || (MediaType.ToString() == "5")) //RADIO AND CABLE
                        {
                            filePath = row["DFile"].ToString();
                            CatName = row["BrandName"].ToString().Replace("/", "");
                            CatName = CatName.Replace(" ", "_"); ;
                            filePathCheck = Server.MapPath("../radio/" + filePath);
                        }

                        else if (MediaType.ToString() == "6") //press
                        {
                            filePath = row["DFile"].ToString();
                            CatName = row["BrandName"].ToString().Replace("/", "");
                            CatName = CatName.Replace(" ", "_"); ;
                            filePathCheck = Server.MapPath("../press/" + filePath);
                        }
                        else if (MediaType.ToString() == "7") //outdoor
                        {
                            filePath = row["NewOutDoorFile"].ToString();
                            filePath1 = row["StreetGraph"].ToString();
                            CatName = row["BrandName"].ToString().Replace("/", "");
                            CatName = CatName.Replace(" ", "_"); ;
                            filePathCheck = Server.MapPath("../outdoor/" + filePath);
                            filePathCheck1 = Server.MapPath("../outdoor-street/" + filePath1);
                        }
                        else if (MediaType.ToString() == "8")//News
                        {
                            filePath = row["CropPicture"].ToString();
                            filePath1 = row["FullPagePicture"].ToString();
                            CatName = row["BrandName"].ToString().Replace("/", "");
                            CatName = CatName.Replace(" ", "_"); ;
                            filePathCheck = Server.MapPath("../news/Cropped/" + filePath);
                            filePathCheck1 = Server.MapPath("../news/FullPage/" + filePath1);
                        }
                        else if (MediaType.ToString() == "9") //internet
                        {
                            filePath = row["DFile"].ToString();
                            CatName = row["BrandName"].ToString().Replace("/", "");
                            CatName = CatName.Replace(" ", "_"); ;
                            filePathCheck = Server.MapPath("../internet/" + filePath);
                        }


                        if (filePath != "")
                        {
                            if (File.Exists(filePathCheck))
                            {
                                //try
                                //{
                                    if (!zip.ContainsEntry( CatName+"/"+filePath))
                                    {
                                        zip.AddFile(filePathCheck, CatName);
                                    }
                                //}
                                //catch { }
                            }
                        }

                        if (filePath1 != "")
                        {
                            if (File.Exists(filePathCheck1))
                            {
                                //try
                                //{
                                

                                    if (!zip.ContainsEntry(CatName+"/"+filePath1))
                                    {
                                        zip.AddFile(filePathCheck1, CatName);
                                    }

                                //}
                                //catch { }
                            }
                        }
                    }
                    //zip.Save(Response.OutputStream);
                //}

                #endregion
            }


        } //Foreach end

        zip.Save(Response.OutputStream);
        Response.Close();
        Response.End();

    }

    protected void btnDownloadAllSummaryAdvertiser_Clicked(object sender, EventArgs e)
    {

        Int32 checkResponse = 0;
        
       
        ZipFile zip = new ZipFile();
        foreach (GridDataItem item in radGridAll.Items)
        {

            Int32 MediaType = 0;
            String CName = String.Empty;
            String CategoryName = String.Empty;

            try
            {
                Label lblMT = (Label)item["MediaType"].FindControl("lblMTN");
                MediaType = Convert.ToInt32(lblMT.Text);
                CName = Utility.GetRequestedVal("adv", "");


            }
            catch
            {

            }
            DataTable dt = RfController.GetVideosAudiosAdvertiserName(MediaType, CName);

            if (dt.Rows.Count > 0)
            {
                #region[Zip Format]
               
                String filePath = String.Empty;
                String filePath1 = String.Empty;

                String downloadFileName = String.Empty;
                 downloadFileName= dt.Rows[0]["AdvertiserName"].ToString().Replace("/", "") + ".zip";
                downloadFileName = downloadFileName.Replace(" ", "_");

                if (checkResponse == 0)
                {
                    Response.Clear();
                    // no buffering - allows large zip files to download as they are zipped
                    Response.BufferOutput = false;
                    Response.ContentType = "application/zip";
                    Response.AddHeader("content-disposition", "filename=" + downloadFileName);
                }
                checkResponse = 1;
                //using (zip = new ZipFile())
                //{
                    foreach (DataRow row in dt.Rows)
                    {
                        String filePathCheck = String.Empty;
                        String filePathCheck1 = String.Empty;

                        String CatName = String.Empty;


                        if (MediaType.ToString() == "1") //TV
                        {
                            filePath = row["DFile"].ToString();
                            CatName = row["AdvertiserName"].ToString().Replace("/", "");
                            CatName = CatName.Replace(" ", "_"); ;
                            filePathCheck = Server.MapPath("../videos/" + filePath);
                        }

                        else if ((MediaType.ToString() == "3") || (MediaType.ToString() == "5")) //RADIO AND CABLE
                        {
                            filePath = row["DFile"].ToString();
                            CatName = row["AdvertiserName"].ToString().Replace("/", "");
                            CatName = CatName.Replace(" ", "_"); ;
                            filePathCheck = Server.MapPath("../radio/" + filePath);
                        }

                        else if (MediaType.ToString() == "6") //press
                        {
                            filePath = row["DFile"].ToString();
                            CatName = row["AdvertiserName"].ToString().Replace("/", "");
                            CatName = CatName.Replace(" ", "_"); ;
                            filePathCheck = Server.MapPath("../press/" + filePath);
                        }
                        else if (MediaType.ToString() == "7") //outdoor
                        {
                            filePath = row["NewOutDoorFile"].ToString();
                            filePath1 = row["StreetGraph"].ToString();
                            CatName = row["AdvertiserName"].ToString().Replace("/", "");
                            CatName = CatName.Replace(" ", "_"); ;
                            filePathCheck = Server.MapPath("../outdoor/" + filePath);
                            filePathCheck1 = Server.MapPath("../outdoor-street/" + filePath1);
                        }
                        else if (MediaType.ToString() == "8")//News
                        {
                            filePath = row["CropPicture"].ToString();
                            filePath1 = row["FullPagePicture"].ToString();
                            CatName = row["AdvertiserName"].ToString().Replace("/", "");
                            CatName = CatName.Replace(" ", "_"); ;
                            filePathCheck = Server.MapPath("../news/Cropped/" + filePath);
                            filePathCheck1 = Server.MapPath("../news/FullPage/" + filePath1);
                        }
                        else if (MediaType.ToString() == "9") //internet
                        {
                            filePath = row["DFile"].ToString();
                            CatName = row["AdvertiserName"].ToString().Replace("/", "");
                            CatName = CatName.Replace(" ", "_"); ;
                            filePathCheck = Server.MapPath("../internet/" + filePath);
                        }


                        if (filePath != "")
                        {
                            if (File.Exists(filePathCheck))
                            {
                                //try
                                //{

                                if (!zip.ContainsEntry(CatName + "/" + filePath))
                                {

                                    zip.AddFile(filePathCheck, CatName);
                                }
                                //}
                                //catch { }
                            }
                        }

                        if (filePath1 != "")
                        {
                            if (File.Exists(filePathCheck1))
                            {
                                //try
                                //{
                                if (!zip.ContainsEntry(CatName + "/" + filePath1))
                                {

                                    zip.AddFile(filePathCheck1, CatName);
                                }
                                //}
                                //catch { }
                            }
                        }
                    }
                    //zip.Save(Response.OutputStream);
                //}

                #endregion
            }


        } //Foreach end

        zip.Save(Response.OutputStream);
        Response.Close();
        Response.End();

    }

    

    protected void btnDownloadAllSummary_Clicked(object sender, EventArgs e)
    {
        int sDate = Convert.ToInt32(Utility.GetRequestedVal("sd", "0"));
        int eDate = Convert.ToInt32(Utility.GetRequestedVal("ed", "0"));
        Int32 checkResponse = 0;

        ZipFile zip = new ZipFile();
        foreach (GridDataItem item in radGridAll.Items)
        {

            Int32 MediaType = 0;
            String CName = String.Empty;
            String CategoryName = String.Empty;

            try
            {
                Label lblMT = (Label)item["MediaType"].FindControl("lblMTN");
                MediaType = Convert.ToInt32(lblMT.Text);
                CName = Utility.GetRequestedVal("cc", "");
              
               
            }
            catch
            {

            }
            DataTable dt = RfController.GetVideosAudiosCategoryName2(MediaType, CName, sDate, eDate);

            if (dt.Rows.Count > 0)
            {
                #region[Zip Format]
              
                String filePath = String.Empty;
                String filePath1 = String.Empty;

                String downloadFileName = String.Empty;
                downloadFileName= dt.Rows[0]["CategoryName"].ToString().Replace("/", "") + ".zip";
                downloadFileName = downloadFileName.Replace(" ", "_");
                if (checkResponse == 0)
                {
                    Response.Clear();
                    // no buffering - allows large zip files to download as they are zipped
                    Response.BufferOutput = false;
                    Response.ContentType = "application/zip";
                    Response.AddHeader("content-disposition", "filename=" + downloadFileName);
                }
                checkResponse = 1;

                //using (zip = new ZipFile())
                //{
                    foreach (DataRow row in dt.Rows)
                    {
                        String filePathCheck = String.Empty;
                        String filePathCheck1 = String.Empty;

                        String CatName = String.Empty;


                        if (MediaType.ToString() == "1") //TV
                        {
                            filePath = row["DFile"].ToString();
                            CatName = row["CategoryName"].ToString().Replace("/", "");
                            CatName = CatName.Replace(" ", "_"); ;
                            filePathCheck = Server.MapPath("../videos/" + filePath);
                        }

                        else if ((MediaType.ToString() == "3") || (MediaType.ToString() == "5")) //RADIO AND CABLE
                        {
                            filePath = row["DFile"].ToString();
                            CatName = row["CategoryName"].ToString().Replace("/", "");
                            CatName = CatName.Replace(" ", "_"); ;
                            filePathCheck = Server.MapPath("../radio/" + filePath);
                        }

                        else if (MediaType.ToString() == "6") //press
                        {
                            filePath = row["DFile"].ToString();
                            CatName = row["CategoryName"].ToString().Replace("/", "");
                            CatName = CatName.Replace(" ", "_"); ;
                            filePathCheck = Server.MapPath("../press/" + filePath);
                        }
                        else if (MediaType.ToString() == "7") //outdoor
                        {
                            filePath = row["NewOutDoorFile"].ToString();
                            filePath1 = row["StreetGraph"].ToString();
                            CatName = row["CategoryName"].ToString().Replace("/", "");
                            CatName = CatName.Replace(" ", "_"); ;
                            filePathCheck = Server.MapPath("../outdoor/" + filePath);
                            filePathCheck1 = Server.MapPath("../outdoor-street/" + filePath1);
                        }
                        else if (MediaType.ToString() == "8")//News
                        {
                            filePath = row["CropPicture"].ToString();
                            filePath1 = row["FullPagePicture"].ToString();
                            CatName = row["CategoryName"].ToString().Replace("/", "");
                            CatName = CatName.Replace(" ", "_"); ;
                            filePathCheck = Server.MapPath("../news/Cropped/" + filePath);
                            filePathCheck1 = Server.MapPath("../news/FullPage/" + filePath1);
                        }
                        else if (MediaType.ToString() == "9") //internet
                        {
                            filePath = row["DFile"].ToString();
                            CatName = row["CategoryName"].ToString().Replace("/", "");
                            CatName = CatName.Replace(" ", "_"); ;
                            filePathCheck = Server.MapPath("../internet/" + filePath);
                        }


                        if (filePath != "")
                        {
                            if (File.Exists(filePathCheck))
                            {
                                //try
                                //{
                                if (!zip.ContainsEntry(CatName + "/" + filePath))
                                {
                                    zip.AddFile(filePathCheck, CatName);
                                }
                                //}
                                //catch { }
                            }
                        }

                        if (filePath1 != "")
                        {
                            if (File.Exists(filePathCheck1))
                            {
                                //try
                                //{
                                if (!zip.ContainsEntry(CatName + "/" + filePath1))
                                {
                                    zip.AddFile(filePathCheck1, CatName);
                                }
                                //}
                                //catch { }
                            }
                        }
                    }
                    //zip.Save(Response.OutputStream);
                //}
               
                 #endregion
            }
           

        } //Foreach end

          zip.Save(Response.OutputStream);
          Response.Close();
          Response.End();
    }

    protected void btnExport_Clicked(object sender, EventArgs e)
    {
        InngridExport();
        objDSExport.SelectParameters["MediaTypeCode"].DefaultValue = Session["mtc"].ToString();
        DataView dv = new DataView();
        dv = (DataView)objDSExport.Select();
        dv.Table.Columns.Remove("TotalRecords");
        DataTable dt = dv.ToTable();
        DumpExcel(dt);
    }
    
    
    protected void btnExportSummary_Clicked(object sender, EventArgs e)
    {

        DataView DvsummaryGet =(DataView) ViewState["dvSummary"];
        try
        {
            DvsummaryGet.Table.Columns.Remove("MediaType");
            DvsummaryGet.Table.Columns.Remove("InvestmentGrid");
        }
        catch { }
        DataTable dtSummary = DvsummaryGet.ToTable();
        dtSummary.Columns["MediaTypeName"].ColumnName = "Media";
        dtSummary.Columns["CountRecord"].ColumnName = "Total Records";
        dtSummary.Columns["Investment"].ColumnName = "Total Investment";

        InngridExport();
        objDSExport.SelectParameters["MediaTypeCode"].DefaultValue = 999.ToString(); //All Case
        DataView dv = new DataView();
        dv = (DataView)objDSExport.Select();
        dv.Table.Columns.Remove("TotalRecords");
        DataTable dt = dv.ToTable();
        DumpExcelSummary(dt, dtSummary);
    }

    private DataTable CreateDatatableofUserSelection()
    {
        String SectorName = String.Empty;
        String SubSectorName = String.Empty;
        String CategoryName = String.Empty;
        String AdvertiserName = String.Empty;
        String AgencyName = String.Empty;
        String BrandName = String.Empty;


        if (Utility.GetRequestedVal("s", "") == "")
        {
            SectorName = "- ALL -";
        }
        else
        {
            SectorName = Utility.GetRequestedVal("s", "");
        }

        if (Utility.GetRequestedVal("ss", "") == "")
        {
            SubSectorName = "- ALL -";
        }
        else
        {
            SubSectorName = Utility.GetRequestedVal("ss", "");
        }

        if (Utility.GetRequestedVal("cc", "") == "")
        {
            CategoryName = "- ALL -";
        }
        else
        {
            CategoryName = Utility.GetRequestedVal("cc", "");
        }

        if (Utility.GetRequestedVal("adv", "") == "")
        {
            AdvertiserName = "- ALL -";
        }
        else
        {
            AdvertiserName = Utility.GetRequestedVal("adv", "");
        }

        if (Utility.GetRequestedVal("brnd", "") == "")
        {
            BrandName = "- ALL -";
        }
        else
        {
            BrandName = Utility.GetRequestedVal("brnd", "");
        }
        
       
        //productCode = Utility.GetRequestedVal("prod", "");


        if (Utility.GetRequestedVal("agn", "") == "")
        {
            AgencyName = "- ALL -";
        }
        else
        {
            AgencyName = Utility.GetRequestedVal("agn", "");
        }
        
        DataTable tableUserSelection = new DataTable();
        tableUserSelection.Columns.Add("Products", typeof(string));
        tableUserSelection.Columns.Add("User Selection", typeof(string));

        tableUserSelection.Rows.Add("Sector", SectorName);
        tableUserSelection.Rows.Add("Sub Sector", SubSectorName);
        tableUserSelection.Rows.Add("Category", CategoryName);
        tableUserSelection.Rows.Add("Advertiser", AdvertiserName);
        tableUserSelection.Rows.Add("Agency", AgencyName);
        tableUserSelection.Rows.Add("Product", BrandName);

        return tableUserSelection;
    }



    private void DumpExcelSummary(DataTable tbl, DataTable tblSummary)
    {
        using (ExcelPackage pck = new ExcelPackage())
        {

            try
            {

                //Create the worksheet
                ExcelWorksheet ws = pck.Workbook.Worksheets.Add("MediaMonitor-Report");

                DataTable UserSelection = CreateDatatableofUserSelection();
               
                ws.Cells["A1"].LoadFromDataTable(UserSelection, true);
               
                ws.Cells["A9"].LoadFromDataTable(tblSummary, true);

                ws.Cells["A17"].LoadFromDataTable(tbl, true);
                ws.DefaultColWidth = 20;
                ws.Cells["A17:I17"].AutoFilter = true;
                using (ExcelRange rng = ws.Cells["A17:I17"])
                {
                    rng.Style.Font.Bold = true;

                }
                using (ExcelRange rngUserSelection = ws.Cells["A1:B1"])
                {
                    rngUserSelection.Style.Font.Bold = true;

                }
                using (ExcelRange rngSummary = ws.Cells["A9:C9"])
                {
                    rngSummary.Style.Font.Bold = true;

                }
            }
            catch { }
            Response.Clear();
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AddHeader("content-disposition", "attachment;  filename=MediaMonitor-Report.xlsx");
            Response.BinaryWrite(pck.GetAsByteArray());
            Response.End();
        }
    }


    private void DumpExcel(DataTable tbl)
{
    using (ExcelPackage pck = new ExcelPackage())
    {
        try
        {
            //Create the worksheet
            ExcelWorksheet ws = pck.Workbook.Worksheets.Add("MediaMonitor-Report");
           
            DataTable UserSelection = CreateDatatableofUserSelection();
           
             ws.Cells["A1"].LoadFromDataTable(UserSelection, true);
           
            ws.Cells["A9"].LoadFromDataTable(tbl, true);
            ws.DefaultColWidth = 20;
            ws.Cells["A9:I9"].AutoFilter = true;

            using (ExcelRange rngUserSelection = ws.Cells["A1:B1"])
            {
                rngUserSelection.Style.Font.Bold = true;

            }
            
            
            using (ExcelRange rng = ws.Cells["A9:I9"])
            {
                rng.Style.Font.Bold = true;

            }
        }
        catch { }
                Response.Clear();       
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.AddHeader("content-disposition", "attachment;  filename=MediaMonitor-Report.xlsx");
                Response.BinaryWrite(pck.GetAsByteArray());
                Response.End();
            }
        }

   
  }
}